package com.nightpiter.view.dialogs;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public abstract class BaseDialog {

	/** application context */
	protected Context context;

	/** the dialog itself */
	private AlertDialog dialog;

	/** builder for the dialog */
	protected AlertDialog.Builder builder;

	/**
	 * public constructor
	 * 
	 * @param context
	 */
	public BaseDialog(Context context) {
		this.context = context;
		this.builder = new AlertDialog.Builder(context);
	}

	/**
	 * set dialog title and icon
	 * 
	 * @param title
	 * @param icon
	 */
	public void setHeader(int title, int icon) {
		this.builder.setTitle(title);
		this.builder.setIcon(icon);
	}

	/**
	 * show the dialog itself
	 */
	protected void show() {
		if (this.dialog == null) {
			this.dialog = this.builder.show();
		}
	}

	/**
	 * hide the dialog if it is visible now
	 */
	public void hide() {
		if (this.dialog != null) {
			this.dialog.dismiss();
			this.dialog = null;
		}
	}

	/**
	 * handles button clicks
	 */
	public void onClick(DialogInterface dialog, int button) {
		if (dialog == this.dialog) {
			this.dialog = null;
		}
	}
	
	protected AlertDialog getDialog() {
		return dialog;
	}

	protected Context getContext() {
		return this.context;
	}

}